package com.gson.token;

import com.gson.token.tokenStorager.TokenStorager;
import com.gson.bean.WeixinAccount;

/**
 * @Company 陕西识代运筹信息科技股份有限公司
 * @Discription
 * @Author houfengli
 * @CreateDate 2017/2/17 14:59
 * @Version 1.0
 */
public class Token {
    private String key;
    private long cacheTime;
    private String value;

    public Token( TokenType tokenType,WeixinAccount account) {
        this.key = tokenType.getKey()+account.getAppId();
        this.cacheTime = tokenType.getCacheTime();
    }
    public Token( TokenType tokenType,String appId) {
        this.key = tokenType.getKey()+appId;
        this.cacheTime = tokenType.getCacheTime();
    }

    public static String getCache(TokenType tokenType, WeixinAccount account) {
        String tokenValue =account.getTokenStorager().getToken(tokenType.getKey()+account.getAppId());
        return tokenValue;
    }

    public static void cache(TokenType tokenType, WeixinAccount account,String tokenStr){
        Token token=new Token(tokenType,account);
        token.setValue(tokenStr);
        account.getTokenStorager().cacheToken(token);
    }

    public static String getCache(TokenType tokenType, String appId,TokenStorager tokenStorager) {
        String tokenValue =tokenStorager.getToken(tokenType.getKey()+appId);
        return tokenValue;
    }

    public static void cache(TokenType tokenType, String appId,TokenStorager tokenStorager,String tokenStr){
        Token token=new Token(tokenType,appId);
        token.setValue(tokenStr);
        tokenStorager.cacheToken(token);
    }

    public String getKey() {
        return key;
    }

    public long getCacheTime() {
        return cacheTime;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }


    public static enum TokenType{
        access_token("access_token:",6600), //微信access_token
        js_access_token("js_access_token:",6600), //js

        wx3p_component_verify_ticket("wx3p_component_verify_ticket:",660),//第三方平台component_access_token
        wx3p_access_token("wx3p_access_token:",6600),//第三方平台access_token
        wx3p_pre_auth_code("wx3p_pre_auth_code:",1100),//第三方平台预授权码
        wx3p_authorizer_access_token("wx3p_authorizer_access_token:",6600)//公众号的接口调用凭据
        ;
        private TokenType(String keyPre,long cacheTime){
            this.key=keyPre;
            this.cacheTime=cacheTime;
        }
        private long cacheTime;
        private String key;

        public long getCacheTime() {
            return cacheTime;
        }

        public String getKey() {
            return key;
        }
    }
}
